# -*- coding:utf-8 -*-

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import xlrd


class Action():
    def __init__(self):
        # 初始化配置，设置Desired Capabilities参数
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "192.168.1.101:5555",
            "appPackage": "com.ss.android.ugc.aweme",
            "appActivity": "com.ss.android.ugc.aweme.main.MainActivity",
            "udid": "192.168.1.101:5555",
            "noReset": "True",
            "newCommandTimeout": "600",
            # "unicodeKeyboard": True,
            # "resetKeyboard": True

        }
        # 指定Appium Server
        self.server = 'http://localhost:4723/wd/hub'
        # 新建一个Session
        self.driver = webdriver.Remote(self.server, self.desired_caps)
        self.driver.implicitly_wait(10)
        time.sleep(5)
        #获取屏幕的高
        self.x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        self.y = self.driver.get_window_size()['height']


    def send_content(self, user_id):
        # 检查搜索框
        print("---------【检查搜索框】------------")
        element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/ahv")))
        # 点击清除
        try:
            print("---------点【清除】------------")
            WebDriverWait(self.driver, 2).until(
                EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/qd"))).click()
        except:
            pass
        # element.send_keys('_13554871018_')
        element.click()
        time.sleep(0.5)
        element.send_keys(user_id)
        time.sleep(0.5)
        print("---------点【回车】------------")
        TouchAction(self.driver).tap(x=657, y=1220).perform()
        # self.driver.execute_script("mobile:performEditorAction", {'action':'search'})
        # self.driver.keyevent(66)  # 搜索键
        # self.driver.press_keycode(66)
        print("---------点【用户标签】------------")
        try:
            WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, '//*[contains(@text, "用户")]'))).click()
        except:
            pass
        try:
            print("---------点【搜索出的用户】------------")
            time.sleep(0.5)
            WebDriverWait(self.driver, 2).until(
                EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/bx3"))).click()
        except:
            pass
        try:
            print("---------检查【用户信息是否加载完成】------------")
            WebDriverWait(self.driver, 1).until(
                EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/cml")))
        except:
            pass
        print("---------点【返回】------------")
        self.driver.keyevent(4)  # 返回
        while True:
            try:
                print("----【循环】-----【检查搜索框】------------")
                WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/ahv")))
                break
            except:
                print("----【循环】-----点【返回】------------")
                self.driver.keyevent(4)
                break
                pass



    '''
    参数 1：driver
    参数 2：t 是持续时间
    参数 3：滑动次数
    '''
    def swipeUp(self, driver, t=500, n=1):
        '''向上滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] *  0.5  # x 坐标
        y1 = l['height'] * 0.75 # 起始 y 坐标
        y2 = l['height'] * 0.25 # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, t)

    def swipeDown(self, driver, t=500, n=1):
        '''向下滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.5 # x 坐标
        y1 = l['height'] * 0.25 # 起始 y 坐标
        y2 = l['height'] * 0.75 # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, 1)


    def swipLeft(self, driver, t=500, n=1):
        '''向左滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.75
        y1 = l['height'] * 0.5
        x2 = l['width']  * 0.05
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)


    def swipRight(self, driver, t=500, n=1):
        '''向右滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.05
        y1 = l['height'] * 0.5
        x2 = l['width']  * 0.75
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)


    def main(self):
        data = xlrd.open_workbook("E:\Desktop\search_keyword.xls")
        table = data.sheets()[0]
        # time.sleep(2)
        # TouchAction(self.driver).tap(x=666, y=91).perform()
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, "com.ss.android.ugc.aweme:id/bjz"))).click()
        for rownum in range(table.nrows):
            if rownum > 0:
                user_id = table.row_values(rownum)[1]
                user_name = table.row_values(rownum)[0]
                print(f"=======行号：【{rownum + 1}】======ID:【{user_id}】====={user_name}========")
                self.send_content(user_id)



if __name__ == '__main__':
    action = Action()
    action.main()
